<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<title>Subir Fotos</title>
		<style type="text/css">
		<!--
		body {
			background-color: #DBE8F9;
			font: 11px/24px "Lucida Grande", "Trebuchet MS", Arial, Helvetica, sans-serif;
			color: #5A698B;
			padding-left:20px;
		}

		#title {
			width: 330px;
			height: 26px;
			color: #5A698B;
			font: bold 11px/18px "Lucida Grande", "Trebuchet MS", Arial, Helvetica, sans-serif;
			padding-top: 5px;
			background: transparent url("images/bg_legend.gif") no-repeat;
			text-transform: uppercase;
			letter-spacing: 2px;
			text-align: left;
		}


		.input {
			background-color: #fff;
			font: 11px/14px "Lucida Grande", "Trebuchet MS", Arial, Helvetica, sans-serif;
			color: #5A698B;
			margin: 4px 0 5px 8px;
			padding: 1px;
			border: 1px solid #8595B2;
		}

		.textarea {
			border: 1px solid #8595B2;
			background-color: #fff;
			font: 11px/14px "Lucida Grande", "Trebuchet MS", Arial, Helvetica, sans-serif;
			color: #5A698B;
			margin: 4px 0 5px 8px;
		}

		.upload_message_success {
			padding:4px;
			background-color:#009900;
			border:1px solid #006600;
			color:#FFFFFF;
			margin-top:10px;
			margin-bottom:10px;
		}

		.upload_message_error {
			padding:4px;
			background-color:#CE0000;
			border:1px solid #990000;
			color:#FFFFFF;
			margin-top:10px;
			margin-bottom:10px;
		}

		-->
		</style>
		<!--link href="style.css" rel="stylesheet" type="text/css" /-->
	</head>
	<body>
	<?php 
		error_reporting(E_ALL);
		session_start();
		$estacionn = $_SESSION['nombre_estacion'];
		$estacion = $_SESSION['estacion'];
	?>
		<div id="title">&nbsp;SUBIR FOTOS EN <?php echo "".$estacionn."";?></div>
		<?php ini_set("memory_limit", "200000000"); // para imagenes grandes y asi no tener problemas con la memoria 

		include "database.php";	
		// upload the file
		if ((isset($_POST["submitted_form"])) && ($_POST["submitted_form"] == "image_upload_form")) 
		{
			// Archivos de tipo jpg gif bmp y png de tamano menor a 4 MB
			if (($_FILES["image_upload_box"]["type"] == "image/jpeg" || 
				 $_FILES["image_upload_box"]["type"] == "image/pjpeg" || 
				 $_FILES["image_upload_box"]["type"] == "image/gif" || 
				 $_FILES["image_upload_box"]["type"] == "image/x-png") && ($_FILES["image_upload_box"]["size"] < 5242880))
			{
 				$max_upload_width = 800;
				$max_upload_height = 600;
		
				// si la imagen fue al servidor
				if($_FILES["image_upload_box"]["type"] == "image/jpeg" || $_FILES["image_upload_box"]["type"] == "image/pjpeg")
				{	
					$image_source = imagecreatefromjpeg($_FILES["image_upload_box"]["tmp_name"]);
					$img_name = $_FILES["image_upload_box"]["name"];
					$file_name = $_FILES["image_upload_box"]["name"];
				}		
		
				if($_FILES["image_upload_box"]["type"] == "image/gif")
				{	
					$image_source = imagecreatefromgif($_FILES["image_upload_box"]["tmp_name"]);
				}	
				
				if($_FILES["image_upload_box"]["type"] == "image/bmp"){	
					$image_source = imagecreatefromwbmp($_FILES["image_upload_box"]["tmp_name"]);
				}			
	
				if($_FILES["image_upload_box"]["type"] == "image/x-png"){
					$image_source = imagecreatefrompng($_FILES["image_upload_box"]["tmp_name"]);
				}
		
				if(!is_dir('../images_fldr/'.$estacionn.'/')) 
					mkdir('../images_fldr/'.$estacionn.'/') or die('No se puede crear directorio');

				$remote_file = "../images_fldr/".$estacionn."/".$_FILES["image_upload_box"]["name"];
				imagejpeg($image_source,$remote_file,100);
				chmod($remote_file,0644);
		
				// saber el tamano de la imagen 
				list($image_width, $image_height) = getimagesize($remote_file);
	
				if($image_width>$max_upload_width || $image_height >$max_upload_height)
				{
					$proportions = $image_width/$image_height;
			
					if($image_width>$image_height){
						$new_width = $max_upload_width;
						$new_height = round($max_upload_width/$proportions);
					}		
					else{
						$new_height = $max_upload_height;
						$new_width = round($max_upload_height*$proportions);
					}		
			
			
					$new_image = imagecreatetruecolor($new_width , $new_height);
					$image_source = imagecreatefromjpeg($remote_file);
			
					imagecopyresampled($new_image, $image_source, 0, 0, 0, 0, $new_width, $new_height, $image_width, $image_height);
					imagejpeg($new_image,$remote_file,100);
			
					imagedestroy($new_image);
				}
				imagedestroy($image_source);

				$descpBox = $_GET["descpBox"];
				header("Location: uploadImgRs.php?upload_message=imagen subido con succeso &upload_message_type=success&show_image=".$_FILES["image_upload_box"]["name"]);
				echo $_FILES["image_upload_box"]["name"];
				exit;
			}
			else
			{
				header("Location: uploadImgRs.php?upload_message=Asegurate q es una imagen de tipo jpg, png o Gif y si es de tamano menor a 5 MB&upload_message_type=error");
				exit;
			}
		}
	?>
    <?php 
		if(isset($_REQUEST['upload_message'])){?>
			<div class="upload_message_<?php echo $_REQUEST['upload_message_type'];?>">
            <?php echo htmlentities($_REQUEST['upload_message']);?>
            </div>
		<?php 
		
		$con = pg_connect("user=".DB_USER." port=".DB_PORT." dbname=".DB_NAME." host=".DB_HOST." password=".DB_PASS)
				or die ("Could not connect to server\n");

		//$img_name = $_FILES["image_upload_box"]["name"];

		$file_name = "../images_fldr/".$estacionn."/". $_REQUEST['show_image'];
		$img = fopen($file_name, 'r') or die("");
		$data = fread($img, filesize($file_name));

		$es_data = pg_escape_bytea($data);

		$query = "INSERT INTO hsig.estacion_imagen(estacion_id, nm_nombre, tipo_imagen, objeto) Values($estacion,'$file_name', 'E', '$es_data')";
		
		if (pg_query($con, $query)) 
		{
			echo '<p>';
			//if(isset($_REQUEST['show_image']) and $_REQUEST['show_image']!=''){}
		?>
		<p>
			<img src="<?php echo $file_name;?>" />	
		</p>
		<?php 
			fclose($img);
			pg_close($con);
			exit;
		}}?>

		<form action="php/uploadImg.php" 
			method="post" 
			enctype="multipart/form-data" 
			name="image_upload_form" 
			id="image_upload_form" 
			target="foo" 
			onSubmit="window.open('', 'foo', 'width=450,height=600,status=yes,resizable=no,scrollbars=no')" >
		<br/>&nbsp;
		<p><br />          
		<input name="submitted_form" type="hidden" id="submitted_form" value="image_upload_form" />
	</form>
</body>
</html>


